package com.hl.sfzy.interceptor;

import com.hl.sfzy.constant.SystemCode;
import com.hl.sfzy.exception.ProjectException;
import com.hl.sfzy.utils.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class TokenHandler implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        System.out.println(request.getRequestURL());
        // 获取请求头中的令牌（用于登录判断）
        String token = request.getHeader("Authentication");
        try{
            // 触发验证
            JWTUtils.verifyToken(token);
            return true;
        }catch (Exception e){
            throw new ProjectException(e.getMessage(), SystemCode.VERIFY_TOKEN_FAIL.getCode());
        }
    }
}